/**
* Project Name:myservice
* Date:2018年12月16日
* Copyright (c) 2018, jingma All Rights Reserved.
*/

package cn.benma666.kettle.ljq;

import cn.benma666.exception.MyException;
import cn.benma666.iframe.DictManager;
import cn.benma666.iframe.Result;
import cn.benma666.km.job.JobManager;
import cn.benma666.myutils.StringUtil;
import cn.benma666.sjsj.web.DefaultLjq;
import cn.benma666.sjsj.web.LjqInterface;
import cn.benma666.sjzt.Db;

import com.alibaba.fastjson.JSONObject;
import com.github.stuxuhai.jpinyin.PinyinException;

/**
 * 参数设置拦截器 <br/>
 * date: 2018年12月16日 <br/>
 * @author jingma
 * @version 
 */
public class JobCsszLjq extends DefaultLjq {

    @Override
    public JSONObject jcxx(JSONObject myparams) {
        sjdx.setDxzt(JobManager.kettledb.getName());
        //获取载体类型
        JSONObject dbObj = DictManager.zdObjByDm(LjqInterface.ZD_SYS_COMMON_SJZT, sjdx.getDxzt());
        if(dbObj==null){
            throw new MyException("该数据对象数据载体无效："+sjdx);
        }
        sjdx.setDxztlx(dbObj.getString("lx"));
        sjdx.setDxgs(dbObj.getString("yhm"));
        return super.jcxx(myparams);
    }
    @Override
    public Result save(JSONObject myParams) {
        JSONObject yobj = myParams.getJSONObject(KEY_YOBJ);
        Db tdb = Db.use(sjdx.getDxzt());
        JSONObject obj = tdb.findFirst("select * from v_job_params t where t.id=?", yobj.getString(FIELD_ID));
        if(StringUtil.isBlank(obj.getString("oid"))){
            //新增
            try {
                tdb.update("insert into job_params(id_job,ocode,value,simple_spell,full_spell) values(?,?,?,?,?)",
                        obj.getIntValue("id_job"),obj.getString("ocode"),yobj.getString("value"),
                        StringUtil.getSimpleSpell(obj.getString("oname")),
                        StringUtil.getFullSpell(obj.getString("oname")));
            } catch (PinyinException e) {
                log.error("插入参数失败",e);
            }
        }else{
            //更新
            tdb.update("update job_params set value=? where id_job=? and ocode=?",
                    yobj.getString("value"), obj.getIntValue("id_job"),obj.getString("ocode"));
        }
        return success("保存成功");
    }
    
}
